
import { observable, action, asMap } from 'mobx';
import * as mobx from 'mobx';
import { WeaForm } from 'comsMobx';
import * as _ from '../util/common';
import { conver2name } from '../util/analyzeUtil'

export class AnalyzeStore {

    @observable tabKey = '1';

    @observable showFieldID = false;     // 显示字段id或字段名

    @observable fieldMap = observable.map ? observable.map({}) : asMap({});

    fieldOptions = [];

    @observable linkage_fieldValue = '';         // 字段值
    @observable linkage_typeValue = '0';         // 联动类型  
    @observable linkage_fieldTypeValue = '0';    // 查询字段类型 0：全部 1：触发 2：赋值

    @observable onlyShowlinkage = false;         //  仅显示联动
    @observable mergeMark = false;               //  联动信息合并标记

    @observable fieldChange_typeValue = '0';
    @observable fieldChange_fieldValue = '';

    @observable formTab = 0;
    constructor() {
    }

    @action
    changeTabKey = (v) => {
        this.tabKey = v;
    }

    @action
    changeShowField = () => {
        this.showFieldID = !this.showFieldID;
    }

    @action
    setShowSearchAd = (bool) => {
        this.showSearchAd = bool;
    }

    @action
    changeShowMerge = () => {
        this.mergeMark = !this.mergeMark;
    }

    @action
    initDatas = () => {
        this.getFieldMap();
        this.getTransferParams();
    }

    @action('所有字段Map')
    getFieldMap = () => {
        const WfForm = window.opener.WfForm;
        const layoutStore = WfForm.getLayoutStore();
        const { tableInfo } = layoutStore;
        !_.isEmpty(tableInfo) && _.forIn(tableInfo, (v, k) => {
            v.fieldinfomap && _.forIn(v.fieldinfomap, (attr, key) => {
                attr["tableMark"] = k;
                this.fieldMap.set(key.toString(), attr);
            })
        })
    }

    @action('查询字段数据')
    getTransferParams = () => {
        let fieldinfomap = [];
        this.fieldMap.forEach((value, key) => {
            fieldinfomap.push({ key: key, showname: conver2name(value.tableMark) + '.' + value.fieldlabel, tableMark: value.tableMark })
        });
        this.fieldOptions = fieldinfomap;
    }

    @action
    linkage_changeFieldValue = (strArr) => {
        this.linkage_fieldValue = strArr;
    }
    @action
    linkTypeChange = (value) => {
        this.linkage_typeValue = value;
    }
    @action
    fieldTypeChange = (value) => {
        this.linkage_fieldTypeValue = value;
    }

    @action
    changeOnlyShowlinkage = () => {
        this.onlyShowlinkage = !this.onlyShowlinkage
    }

    fieldChange_typeChange = (value) => {
        this.fieldChange_typeValue = value;

    }
    fieldChange_changeFieldValue = (strArr) => {
        this.fieldChange_fieldValue = strArr;
    }
    changeFormTab = (key) => {
        this.formTab = key;
    }
    getOptions = (options) => {
        let retArr = [];
        if (this.formTab === 0) {
            retArr = options;
        } else if (this.formTab === 1) {
            retArr = options.filter(item => {
                return item.tableMark == "main";
            })
        } else {
            retArr = options.filter(item => {
                return item.tableMark == "detail_" + (this.formTab - 1);
            })
        }
        return retArr;
    }
}
